package com.itheima.reggie.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R save(@RequestBody SetmealDto setmealDto){
        return setmealService.saveWithDish(setmealDto);
    }

    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R page(Integer page,Integer pageSize,String name){
        IPage<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<SetmealDto> SetmealDtopage = new Page<>();
        LambdaUpdateWrapper<Setmeal> lqw = new LambdaUpdateWrapper<>();
        lqw.like(StrUtil.isNotEmpty(name), Setmeal::getName,name);
        lqw.eq(Setmeal::getIsDeleted,0);
        setmealService.page(setmealPage,lqw);
        BeanUtils.copyProperties(setmealPage,SetmealDtopage,"records");
        long total = setmealPage.getTotal();
        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> setmealDtos=records.stream().map((setmeal)->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            Long categoryId = setmealDto.getCategoryId();
            Category category = categoryService.getById(categoryId);
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());
        /*for (Setmeal setmeal : records) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            Long categoryId = setmealDto.getCategoryId();
            Category category = categoryService.getById(categoryId);
            setmealDto.setCategoryName(category.getName()); //分类名称
            //setmealDto.setSetmealDishes(null); //套餐对应的菜品的集合，貌似用不到
        }*/
        SetmealDtopage.setRecords(setmealDtos);
        return R.success(SetmealDtopage);
    }

    /**
     * 套餐删除、批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R deleteByIds(@RequestParam List<Long> ids){
        if (ids.isEmpty()){
            return R.error("批量操作，请先选择套餐");
        }
        LambdaUpdateWrapper<Setmeal> lqw = new LambdaUpdateWrapper<>();
        lqw.set(Setmeal::getIsDeleted,1);
        lqw.in(Setmeal::getId, ids);
        lqw.eq(Setmeal::getStatus, 0);
        boolean update = setmealService.update(lqw);
        return update?R.success("删除成功"): R.error("删除失败");
    }

    /**
     * 起售/停售 批量起售/批量停售
     * @param ids
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    public R status(@RequestParam List<Long> ids,@PathVariable Integer status){
        if (ids == null || status == null){
            return R.error("批量操作，请先选择套餐");
        }
        LambdaUpdateWrapper<Setmeal> lqw = new LambdaUpdateWrapper<>();
        lqw.set(Setmeal::getStatus,status);
        lqw.in(Setmeal::getId, ids);
        setmealService.update(lqw);
        return R.success("操作成功");
    }

    /**
     * 修改套餐
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R update(@RequestBody SetmealDto setmealDto){
        return setmealService.updateWithDish(setmealDto);
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.getByIdWithDish(id);
        return R.success(setmealDto);
    }

    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        //创建推荐构造器
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId,setmeal.getCategoryId());
        lqw.eq(setmeal.getStatus() != null, Setmeal::getStatus,setmeal.getStatus());
        lqw.eq(Setmeal::getIsDeleted,0);
        lqw.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(lqw);
        return R.success(list);
    }
}
